package com.iespring.controller.app.admin;

import com.iespring.bean.app.user.vo.WxLoginVo;
import com.iespring.bean.app.user.vo.WxUserHomeIndexVo;
import com.iespring.bean.app.user.vo.WxUserOrderVo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.config.shiro.MallToken;
import com.iespring.service.app.WxUserService;
import com.iespring.util.Md5Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author xD
 */
@RestController
@RequestMapping("wx")
public class WxUserController {
    @Resource
    private WxUserService wxUserService;

    @PostMapping("auth/login")
    public BaseRespVo login(@RequestBody Map map) throws Exception {
        String username = (String) map.get("username");
        String originPassWord = (String) map.get("password");
        String password = Md5Utils.getMd5(originPassWord);
        UsernamePasswordToken wx = new MallToken(username, password, "wx");
        WxLoginVo wxLogin = wxUserService.login(wx);
        if (!ObjectUtils.isEmpty(wxLogin)) {
            return BaseRespVo.ok(wxLogin);
        } else {
            return BaseRespVo.unAuthc();
        }
    }

    /**
     * 登出
     *
     * @since 2022/12/31 20:27
     */
    @PostMapping("auth/logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

    /**
     * 登录后的index
     *
     * @since 2022/12/31 21:39
     */
    @GetMapping("user/index")
    public BaseRespVo userIndex() {
        WxUserOrderVo wxUserIndexVo = wxUserService.userIndex();
        return BaseRespVo.ok(wxUserIndexVo);
    }

    /**
     * 退出登录index
     *
     * @since 2022/12/31 21:40
     */
    @GetMapping("home/index")
    public BaseRespVo homeIndex() {
        WxUserHomeIndexVo wxUserHomeIndexVo = wxUserService.homeIndex();
        return BaseRespVo.ok(wxUserHomeIndexVo);
    }

}
